<?php defined('SYSPATH') OR die('No direct access allowed.');

class Model_User extends Model_Auth_User {

	//protected $_has_one = array('profile' => array());
	
	public function validate_create(& $array) 
	{	
		$array = Validate::factory($array)
						->rules('password', $this->_rules['password'])
						//->rules('username', $this->_rules['username'])
						->rules('email', $this->_rules['email'])
						->rules('password_confirm', $this->_rules['password_confirm'])
						//->filter('username', 'trim')
						->filter('email', 'trim')
						->filter('password', 'trim')
						->filter('password_confirm', 'trim')
						->filter('imie', 'trim')
						->filter('nazwisko', 'trim')
						->filter('lang', 'trim');
 
		/* foreach($this->_callbacks['username'] as $callback){
			$array->callback('username', array($this, $callback));
		} */
 
		foreach($this->_callbacks['email'] as $callback){
			$array->callback('email', array($this, $callback));
		}		
		return $array;
	}
	
	public function validate_change_admin(& $array, $save = FALSE)
	{	
		$array = Validate::factory($array)
				->rules('email', $this->_rules['email'])
				->rules('username', $this->_rules['username'])					
				->filter('email', 'trim')
				->filter('imie', 'trim')
				->filter('nazwisko', 'trim')
				//->filter('info', 'trim')
				//->filter('lang', 'trim')
				
				->filter('username', 'trim');
 
		if(trim($array['password']) != '')
			$array
			->rules('password', array('min_length'=> array(5), 'max_length'=>array(42)))
			->filter('password', 'trim')
			->rules('password_confirm', $this->_rules['password_confirm'])
			->filter('password_confirm', 'trim');

		return $array;		
	}
	
	public function validate_change(& $array, $save = FALSE)
	{	
		$array = Validate::factory($array)
				->rules('email', $this->_rules['email'])
				//->rules('username', $this->_rules['username'])					
				->filter('email', 'trim')
				->filter('imie', 'trim')
				->filter('nazwisko', 'trim')
				//->filter('info', 'trim')
				//->filter('lang', 'trim')
				
				//->filter('username', 'trim')
				->callback('email', array($this, 'email_change'));
				//->callback('username', array($this, 'username_change'));
 
		if(trim($array['password']) != '')
			$array
			->rules('password', array('min_length'=> array(5), 'max_length'=>array(42)))
			->filter('password', 'trim')
			->rules('password_confirm', $this->_rules['password_confirm'])
			->filter('password_confirm', 'trim');
		return $array;		
	}
	
	public function email_change(Validate $array, $field)
	{
		
		$exists = (bool) DB::select(array('COUNT("*")', 'total_count'))
						->from($this->_table_name)
						->where('email',   '=',   $array[$field])
						//->where('id',     '!=',   Session::instance()->get('user_temp'))
						->where('id',     '!=',   Auth::instance()->get_user()->id)
						->execute($this->_db)
						->get('total_count');
		if ($exists)
			$array->error($field, 'email_change', array($array[$field]));
			
	}
	
	/* public function username_change(Validate $array, $field)
	{
		$exists = (bool) DB::select(array('COUNT("*")', 'total_count'))
						->from($this->_table_name)
						->where('username',   '=',   $array[$field])
						//->where('id',     '!=',   Session::instance()->get('user_temp'))
						->where('id',     '!=',   Auth::instance()->get_user()->id)
						->execute($this->_db)
						->get('total_count');
		if ($exists)
			$array->error($field, 'username_change', array($array[$field]));
			
	} */
	public function get_role()
	{
		$my_roles = $this->roles->find_all();
		print_r($my_roles);
	}
	
}